package View.StoreManage.sShowData;

import View.StoreManage.sMenu.sMenu;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import sx.dao.GsalesDao;
import sx.entity.Gsales;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;

/*?????????????*/
public class sShowData extends Application implements Initializable {


    @FXML
    private TableView<Gsales> sd1;

    @FXML
    private TableColumn<String, String> gsidColumn;

    @FXML
    private TableColumn<String, String> snameColumn;

    @FXML
    private TableColumn<String, String> sidColumn;

    @FXML
    private TableColumn<String, String> snumColumn;

    @FXML
    private Button back;

    @FXML
    private TableColumn<String, String> sdateColumn;

    @FXML
    private TableColumn<String, String> gidColumn;



    @Override
    public void start(Stage primaryStage) {
        try {
            Class clazz = getClass();
            Parent parent = FXMLLoader.load(clazz.getResource("sShowData.fxml"));
            Scene scene = new Scene(parent, 600, 600);
            scene.getStylesheets().add(getClass().getResource("/sources/Css/application1.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showList(){

        ObservableList<Gsales> list = FXCollections.observableArrayList();
        ArrayList<Gsales> bList = new GsalesDao().dailyGsales();//

        gsidColumn.setCellValueFactory(new PropertyValueFactory("gsid"));//
        gidColumn.setCellValueFactory(new PropertyValueFactory("gId"));
        sidColumn.setCellValueFactory(new PropertyValueFactory("sId"));//
        sdateColumn.setCellValueFactory(new PropertyValueFactory("sDate"));
        snameColumn.setCellValueFactory(new PropertyValueFactory("gName"));//
        snumColumn.setCellValueFactory(new PropertyValueFactory("sNum"));



        //EntityList sales=new EntityList();
        Iterator<Gsales> iterator=bList.iterator();
        while(iterator.hasNext()){
            Gsales sales =iterator.next();//
            list.add(sales);        //st
        }
        System.out.println(list);
        sd1.setItems(list); //tableview???list
    }

    @FXML

    public void sBack(){
        try {
            new sMenu().start(new Stage());

        } catch (Exception e) {
            // TODO: handle exception
        }
        exitButtonOnMouseClicked(back);

    }


    //????????
    private void exitButtonOnMouseClicked(Button exitButton) {
        Stage stage = (Stage) exitButton.getScene().getWindow();
        stage.close();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        showList();
    }


}
